package top.tagao.m03_beHavioralPatterns.b_13chain.middleware;

/**
 * 检查请求数量限制
 * @author Viktor.Liu
 * @Date 2023/4/28 17:12
 */
public class ThrottlingMiddleware extends  MiddleWare {

    private int requestPerMinute;

    private int request;

    private long currentTime;

    public ThrottlingMiddleware(int requestPerMinute) {
        this.requestPerMinute = requestPerMinute;
        this.currentTime =System.currentTimeMillis();
    }


    @Override
    public boolean handle(String email, String password) {
        if (System.currentTimeMillis()  > currentTime + 60000){
            request = 0;
            currentTime =System.currentTimeMillis();
        }
        request ++;
        if(request > requestPerMinute){
            System.out.println("Request limit exceeded!");
            return false;
        }
        return  handleNext(email,password);
    }
}
